from wordcloud import WordCloud
import numpy as np
from matplotlib import pyplot as plt
import sqlite3
import jieba
from PIL import Image

# 准备词云所需的文字
con = sqlite3.connect('web_movie\movie.db')
cur = con.cursor()
sql = 'select 影片概况 from movie250'
data = cur.execute(sql)
text = ''
for item in data:
    text = text + item[0][1:-1]
    # print(item[0][1:-1])
# print(text)
cur.close()
con.close()

# 文字分词
cut = jieba.cut(text)
string = ' '.join(cut)
# print(string)

img = Image.open(r'web_movie\111.jpg')  #打开遮罩图片
img_array = np.array(img)   #将图片转换为数组
# print(img_array)
wc = WordCloud(
    background_color='white',
    mask=img_array,
    font_path='STXINGKA.TTF',   # 、字体所在位置：C:\Windows\Fonts
)
wc.generate_from_text(string)

# 绘制图片
fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off') #不显示坐标轴
# plt.show()
plt.savefig('ciyun.png')
